package com.readnote.common.action;

import com.cr.backbone.action.AppBaseAction;
import com.cr.backbone.action.AppBaseActionSupport;
import com.cr.backbone.action.LoginInfo;
import com.cr.backbone.intercepter.SessionInterceptKeys;
import com.opensymphony.xwork2.ActionContext;
import com.readnote.user.model.User;
import com.readnote.user.model.UserInfo;
import com.readnote.user.service.UserInfoService;
import com.readnote.user.service.UserService;


public class LoginAction extends AppBaseActionSupport{
	
	private User user;
	private UserService userService;
	private UserInfoService userInfoService;

	
	/**
	 * 跳转个人登录页面
	 * @return
	 */
	public String login(){
		return INPUT;   
	}
	
	public String logon(){
		//1 空验证
		//2 用户或者密码错误
		String _loginName = user.getLoginName();
		String _password = user.getPassword();
		
		//3 登录验证
		String value="用户登录名或者密码错误!";
		User _user = userService.getUserByLoginNameAndPassword(_loginName,_password);
		if(_user==null){
			value="用户或者密码错误!";
			this.addFieldError("user.loginName", value);
			ActionContext.getContext().put("logon_error", value);
			return INPUT;
		}
		
		//4 登录成功处理
		
//		message = ActionUtils.setMessageForAction(message,"欢迎登录readnote系统进行学习！");
		setMessage( "欢迎登录readnote系统进行学习！" );
		
		// 登录信息自动加载到session
		setLoginInfoSetInSession(_user);
		
		return SUCCESS;
	}
	
	
	/**
	 * 
	 * 登录信息LoginInfo，加载到session中
	 * @param _user
	 */
	private void setLoginInfoSetInSession(User _user){
		// 登录信息
		UserInfo _userInfo=userInfoService.getUserInfoByUser(_user);
		LoginInfo loginInfo=new LoginInfo();
		loginInfo.login(_user, _userInfo);
		ActionContext.getContext().getSession().put(SessionInterceptKeys.LOGIN_USER_LOGIN_INFO_KEY, loginInfo);
	}
	
	/**
	 * 登录信息LoginInfo，从session移除
	 */
	private void setLoginInfoSetOutSession(){
		// 登录信息
		LoginInfo loginInfo = (LoginInfo) ActionContext.getContext().getSession().get(SessionInterceptKeys.LOGIN_USER_LOGIN_INFO_KEY);
		if(loginInfo !=null ){
			loginInfo.logonOut();
			ActionContext.getContext().getSession().put(SessionInterceptKeys.LOGIN_USER_LOGIN_INFO_KEY,loginInfo);
		}
	}
	
	public String logonout(){
		//4 登出处理
		setLoginInfoSetOutSession();
		return INPUT;
	}
	
//	/**
//	 * 验证Action 所有方法
//	 * @methodDescribe
//	 * @author xianghua.hu
//	 * @date 2012-3-4 上午11:17:47
//	 */
//	public void validate(){
//		if(user ==null || user.getLoginName() ==null || "".equals(user.getLoginName().trim()))
//			this.addFieldError("user.loginName", "登录名不存在");
//		else if(user.getPassword() ==null || "".equals(user.getPassword().trim()))
//			this.addFieldError("user.password", "密码不存在");
//	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	

	
}
